package 第一题;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ServerThread implements Runnable{
    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        ObjectOutputStream oos = null;
        InputStream is = null;
        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            oos = new ObjectOutputStream(socket.getOutputStream());
            is = socket.getInputStream();
            while (true) {
                String num = br.readLine();
                if (num.equals("0")) {
                    System.out.println(socket.getInetAddress() + "\t这个用户已经退出");
                   return;
                } else if (num.equals("1")) {
                    System.out.println("他要查询全部学生信息");
                    ArrayList<Student> list = (ArrayList<Student>) StudentBusiness.showAllStudent();
                    oos.writeObject(list);
                } else if (num.equals("2")) {
                    System.out.println("他要根据查询部分学生信息");
                    String name = br.readLine();
                    List<Student> studentList = StudentBusiness.findByName(name);
                    oos.writeObject(studentList);
                    oos.flush();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
